//
//  SetViewController.m
//  何五路
//
//  Created by _ADY on 15/7/27.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "SetViewController.h"

@interface SetViewController ()

@end
static LoadingView *loading = nil;
@implementation SetViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"设置";
        self.view.backgroundColor = bgColor;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    UITableView *mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height)];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    aArray = [NSArray arrayWithObjects:@"2G/3G网络手动下载图片",@"新浪微博",@"检查更新",@"清除本地缓存", nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isLoginKey"])
        return 5;
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SetCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
        //        cell.tintColor = [UIColor colorWithRed:243/255.0 green:125/255.0 blue:125/255.0 alpha:1];//勾选颜色
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.row == 4)
    {
        cell.backgroundColor = [UIColor clearColor];
        UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [bgButton setFrame:CGRectMake(20, 10, screenMySize.size.width-40, 40)];
        [bgButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [bgButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [bgButton setBackgroundImage:[UIImage imageNamed:@"enButtons"] forState:UIControlStateNormal];
        [bgButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:bgButton];
        return cell;
    }
    if (indexPath.row == 0) {
        cell.accessoryType =  UITableViewCellAccessoryNone;
        UISwitch *swi1=[[UISwitch alloc]init];
        swi1.frame=CGRectMake(screenMySize.size.width-60, 7, 60, 40);
        swi1.onTintColor=[UIColor redColor];
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"switchBoolSet"])
        {
            [swi1 setOn:NO animated:YES];
        }
        else
            [swi1 setOn:YES animated:YES];
        swi1.tag = indexPath.row +10;
        [swi1 addTarget:self action:@selector(getValue:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:swi1];
    }
    else
    {
//        cell.detailTextLabel.text = @"123";
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [aArray objectAtIndex:indexPath.row];
    
    
    // Configure the cell...
    return cell;
}

-(void)getValue:(id)sender{
    UISwitch *swi2=(UISwitch *)sender;
    
    [[NSUserDefaults standardUserDefaults] setBool:swi2.isOn forKey:@"switchBoolSet"];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 1 && alertView.tag == 1)
    {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[SendService VarUrl]]];
        return;
        
    }
    if (buttonIndex == 1) {
        [SendService setPassword:@""];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLoginKey"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kTreatedCompleteNotification1 object:self];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.row == 2)//更新
    {
        loading = [[LoadingView alloc] initWithFrame:self.view.frame];
        [self.view addSubview:loading];
        NSMutableDictionary *postDic2 = [[NSMutableDictionary alloc] init];
        [postDic2 setObject:@"ios" forKey:@"os"];
        [postDic2 setObject:_nerVion forKey:@"ver"];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSDictionary *myArray = [SendService CheckVer:postDic2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [loading closeLoading];
                if (![[NSString stringWithFormat:@"%@",[myArray objectForKey:@"Result"]] isEqualToString:@"error"])
                {

//                    NSLog(@"myArray %@",myArray);
                    if ([[NSString stringWithFormat:@"%@",[[myArray objectForKey:@"Entity"] objectForKey:@"Ver"]] isEqualToString:_nerVion])
                    {
                        [SendService alerViewMessage:mVar];
                    }
                    else
                    {
                        [SendService setVarUrl:[NSString stringWithFormat:@"%@",[[myArray objectForKey:@"Entity"] objectForKey:@"AppUrl"]]];
                        NSString *mStr = [NSString stringWithFormat:@"%@",[[myArray objectForKey:@"Entity"] objectForKey:@"Description"]];
                        if (mStr.length == 0 || [mStr isEqualToString:@"(null)"]) {
                            mStr = @"暂无描述";
                        }
                        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"确定升级到最新版本?"
                                                                        message:mStr
                                                                       delegate:self
                                                              cancelButtonTitle:@"取消"
                                                              otherButtonTitles:@"更新",nil];
                        alert.tag = 1;
                        [alert show];
                    }
                }

            });
        });
    }
}
-(void)loginAction
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"确定退出登录？"  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    [alert show];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
